package cn.stronglink.asset.module.report.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetBrand;
import cn.stronglink.asset.model.AssetModel;
import cn.stronglink.asset.model.AssetType;
import cn.stronglink.asset.model.Department;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.ReportFile;
import cn.stronglink.asset.model.ReportPlan;
import cn.stronglink.asset.model.User;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.AssetExlVo;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.basicdata.assetBrand.mapper.AssetBrandMapper;
import cn.stronglink.asset.module.basicdata.assetModel.mapper.AssetModelMapper;
import cn.stronglink.asset.module.basicdata.assetType.mapper.AssetTypeMapper;
import cn.stronglink.asset.module.basicdata.tRoom.mapper.TRoomMapper;
import cn.stronglink.asset.module.report.mapper.ReportMapper;
import cn.stronglink.asset.module.report.vo.ConditionVo;
import cn.stronglink.asset.module.report.vo.ExlChartVo;
import cn.stronglink.asset.module.report.vo.ReportFileListVo;
import cn.stronglink.asset.module.report.vo.ReportParams;
import cn.stronglink.asset.module.report.vo.ReportTempletVo;
import cn.stronglink.asset.module.sys.department.mapper.DepartmentMapper;
import cn.stronglink.asset.module.sys.user.mapper.UserMapper;
import cn.stronglink.suite.core.schedulder.TaskScheduled;
import cn.stronglink.suite.core.schedulder.TaskScheduled.JobType;
import cn.stronglink.suite.core.schedulder.TaskScheduled.TaskType;
import cn.stronglink.suite.core.support.scheduler.SchedulerManager;
import cn.stronglink.suite.core.util.DownloadExcelUtil;
import cn.stronglink.suite.core.util.SystemConfigurer;
import cn.stronglink.suite.core.util.WebUtil;
import jxl.format.BorderLineStyle;

@Service
@Transactional(rollbackFor=Exception.class) 
public class ReportService {
	
	@Autowired
	private ReportMapper reportMapper;
	@Autowired
	private SchedulerManager schedulerManager;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private TRoomMapper tRoomMapper;
	@Autowired
	private AssetTypeMapper assetTypeMapper;
	@Autowired
	private AssetBrandMapper assetBrandMapper;
	@Autowired
	private AssetModelMapper assetModelMapper;
	@Autowired
	private DepartmentMapper departmentMapper;
	@Autowired
	private UserMapper userMapper;


	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ReportTempletVo> queryList(Page<ReportTempletVo> page, Map<String, Object> params) {
		List<ReportTempletVo> list =reportMapper.queryList(page,params);
		page.setRecords(list);
		return page;	
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public ReportTempletVo qryById(Long id) {
		ReportTempletVo vo = reportMapper.qryById(id);
		if(vo!=null) {	
			ConditionVo bm = JSON.parseObject(vo.getCondition(), ConditionVo.class);
            if(bm.getRoom_id()!=null) {
            	String roomName =tRoomMapper.qryById(bm.getRoom_id());
            	bm.setRoomName(roomName);
			}
            if(bm.getBuy_time()!=null) {
            	SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
            	bm.setBuyTime1(dateFm.format(bm.getBuy_time().get(0))+"~"+dateFm.format(bm.getBuy_time().get(1)));
            }
            if(bm.getExpiration_time()!=null) {
            	SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
            	bm.setExpirationTime1(dateFm.format(bm.getExpiration_time().get(0))+"~"+dateFm.format(bm.getExpiration_time().get(1)));
            }
			if(bm.getRack_id()!=null) {
				ItAsset rack=itassetMapper.qryRackNameById(bm.getRack_id().toString());
				bm.setRackName(rack!=null?rack.getAssetName():"");
			}
			if(bm.getType_id()!=null) {
				AssetType type =assetTypeMapper.qryById(bm.getType_id());	
				bm.setTypeName(type!=null?type.getName():"");
			}
			if(bm.getBrand_id()!=null) {
				AssetBrand brand =assetBrandMapper.qryById(bm.getBrand_id());
				bm.setBrandName(brand!=null?brand.getName():"");
			}
			if(bm.getModel_id()!=null) {
				AssetModel model =assetModelMapper.qryById(bm.getModel_id());
				bm.setModelName(model!=null?model.getName():"");
			}
			if(bm.getManage_branch_id()!=null) {
				Department department =departmentMapper.qryDeptById(bm.getManage_branch_id());
				bm.setManageBranchName(department!=null?department.getName():"");
			}
			if(bm.getUse_branch_id()!=null) {
				Department department =departmentMapper.qryDeptById(bm.getUse_branch_id());
				bm.setUseBranchName(department!=null?department.getName():"");
			}
			if(bm.getEnd_user()!=null) {
				User user =userMapper.qryUserById(bm.getEnd_user());
				bm.setEndUserName(user!=null?user.getName():"");
			}
			vo.setConditionVo(bm);
			List<ReportPlan> planList = reportMapper.qryPlanListByTempletId(id);
			if (planList!=null&&planList.size()>0) {
				vo.setReportPlanList(planList);
			}
		}
		
		return vo;
	}

	public void create(ReportTempletVo reportTempletVo) {
		reportTempletVo.setId(IdWorker.getId());
		reportTempletVo.setCreateBy(WebUtil.getCurrentUser());
		reportTempletVo.setCreateTime(new Date());
		reportMapper.create(reportTempletVo);
		if (reportTempletVo.getReportPlanList()!=null&&reportTempletVo.getReportPlanList().size()>0) {
			for (ReportPlan reportPlan : reportTempletVo.getReportPlanList()) {
				reportPlan.setId(IdWorker.getId());
				reportPlan.setTempletId(reportTempletVo.getId());
				reportPlan.setCreateBy(WebUtil.getCurrentUser());
				reportPlan.setCreateTime(new Date());
				reportPlan.setTempletId(reportTempletVo.getId());
			}
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("reportPlans", reportTempletVo.getReportPlanList());
			reportMapper.addReportPlan(map);
		}
		if (reportTempletVo.getType().equals(2)) {
			getTaxk(reportTempletVo);
		}
	}

	public void edit(ReportTempletVo reportTempletVo) {
		List<ReportPlan> planList = reportMapper.qryPlanListByTempletId(reportTempletVo.getId());
		if (planList!=null&&planList.size()>0) {
			for (ReportPlan o : planList) {
				TaskScheduled task = new TaskScheduled();
				task.setTaskName(o.getId().toString());
				task.setTaskGroup("report");
				schedulerManager.delJob(task);
			}
		}
		reportTempletVo.setUpdateTime(new Date());
		reportTempletVo.setUpdateBy(WebUtil.getCurrentUser());
		reportMapper.edit(reportTempletVo);
		reportMapper.delReportPlanByTempletId(reportTempletVo.getId());
		if (reportTempletVo.getReportPlanList()!=null&&reportTempletVo.getReportPlanList().size()>0) {
			for (ReportPlan reportPlan : reportTempletVo.getReportPlanList()) {
				reportPlan.setId(IdWorker.getId());
				reportPlan.setCreateBy(WebUtil.getCurrentUser());
				reportPlan.setCreateTime(new Date());
				reportPlan.setTempletId(reportTempletVo.getId());
			}
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("reportPlans", reportTempletVo.getReportPlanList());
			reportMapper.addReportPlan(map);
		}
		if (reportTempletVo.getType().equals(2)) {
			getTaxk(reportTempletVo);
		}
	}

	public void remove(Long id) {
		List<ReportPlan> planList = reportMapper.qryPlanListByTempletId(id);
		if (planList!=null&&planList.size()>0) {
			for (ReportPlan o : planList) {
				TaskScheduled task = new TaskScheduled();
				task.setTaskName(o.getId().toString());
				task.setTaskGroup("report");
				schedulerManager.delJob(task);
			}
		}
		reportMapper.remove(id);
		reportMapper.delReportPlanByTempletId(id);
	}
	
	private void getTaxk(ReportTempletVo reportTempletVo) {
		if (reportTempletVo.getReportPlanList() != null && reportTempletVo.getReportPlanList().size() > 0) {
			for (ReportPlan time : reportTempletVo.getReportPlanList()) {
				int hh=0,ss=0;
				String hs[]=time.getTime().split(":");
				if(hs!=null&&hs.length>0){
					hh=hs[0]!=null?Integer.parseInt(hs[0]):0;
					ss=hs[1]!=null?Integer.parseInt(hs[1]):0;
					String taskCron="";
					if(reportTempletVo.getCycleType()==1){
						//每天
						taskCron="0 "+ss+" "+hh+" * * ? *";
					}else if(reportTempletVo.getCycleType()==2){
						//每周
						String[] weeks={"MON","TUES","WED","THUR","FRI","SAT","SUN"};
						taskCron="0 "+ss+" "+hh+" ? * "+weeks[time.getDay()];
					}else if(reportTempletVo.getCycleType()==3){
						//每月
						taskCron="0 "+ss+" "+hh+" "+time.getDay()+" * ?";
				    }
					//创建新的定时器任务
					TaskScheduled task = new TaskScheduled();
					task.setTaskName(time.getId().toString());
					task.setTaskGroup("report");
					task.setStatus("1");
					//taskCron="0 0/1 * * * ?";     0 30,39 12 7,10 12 ? 2017      0 30 11 7 12 ? 2017
					task.setTaskCron(taskCron);
					task.setTaskDesc(reportTempletVo.getName());
					task.setJobType(JobType.job);
					task.setTaskType(TaskType.local);
					task.setTargetObject("reportService");
					task.setTargetMethod("buildReportFile");
					task.setArgs(reportTempletVo.getId());
					schedulerManager.updateTask(task);
				}
			}
		}
	}

	public Page<ReportFileListVo> queryReportFileList(Page<ReportFileListVo> page, Map<String, Object> params) {
		List<ReportFileListVo> list =reportMapper.queryReportFileList(page,params);
		if (list != null && list.size() > 0 ) {
			for (ReportFileListVo reportFileListVo : list) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				String timeString = sdf.format(reportFileListVo.getCreateTime());
				String tempLetName = reportFileListVo.getFileName();
				StringBuffer sb = new StringBuffer();
				sb.append(tempLetName);
				sb.append("_");
				sb.append(timeString);
				reportFileListVo.setFileName(sb.toString());
				String reportExcelpath=(String)SystemConfigurer.getContextProperty("report_excel_path");
				reportFileListVo.setFilePath(reportExcelpath+"/"+reportFileListVo.getFilePath());
			}
		}
		page.setRecords(list);
		return page;	
	}

	public void removeFile(ReportFile reportFile) {
		reportMapper.removeFile(reportFile.getId());
	}

	public void buildReportFile(Long id) throws Exception{
		ReportTempletVo vo = reportMapper.qryById(id);
		ReportParams params = JSONObject.parseObject(vo.getCondition(), ReportParams.class);
		if (params.getBuy_time()!=null&&params.getBuy_time().size()==2) {
			params.setBuy_time_start(params.getBuy_time().get(0));
			params.setBuy_time_end(params.getBuy_time().get(1));
		}
		if (params.getExpiration_time()!=null&&params.getExpiration_time().size()==2) {
			params.setExpiration_time_start(params.getExpiration_time().get(0));
			params.setExpiration_time_end(params.getExpiration_time().get(1));
		}
		List<ItAssetVo> allList = itassetMapper.queryAssetListForReport(params);
		DownloadExcelUtil dExlUtile = null;
		//在磁盘中写入exl。传入当前type的name，和文件名：DownloadExcelUtil
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = String.valueOf(id)+"_"+sdf.format(new Date());
		String reportExcelpath=(String)SystemConfigurer.getContextProperty("report_excel_path");
		String yearString = new SimpleDateFormat("yyyy").format(new Date());
		String monthString = new SimpleDateFormat("MM").format(new Date());
		String dbFilePath = "/"+yearString+"/"+monthString+"/"+fileName+".xls";
		File f = new File(reportExcelpath+"/"+yearString+"/"+monthString);
		if(!f.exists()){
			f.mkdirs();
		}
		dExlUtile = new DownloadExcelUtil(null,reportExcelpath+"/"+yearString+"/"+monthString+"/"+fileName,"数据");
		int dataNum = 1;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		List<String> imgPathList = new ArrayList<String>();
		if (allList!=null && allList.size()>0) {
			for (ItAssetVo itAssetVo : allList) {
				StringBuffer sb = new StringBuffer();
				sb.append("资产编号_");
				sb.append(itAssetVo.getAssetNo()!=null?itAssetVo.getAssetNo():"-");
				sb.append("$");
				sb.append("设备序列号_");
				sb.append(itAssetVo.getSerialNo()!=null?itAssetVo.getSerialNo():"-");
				sb.append("$");
				sb.append("资产分类_");
				sb.append(itAssetVo.getTypeName()!=null?itAssetVo.getTypeName():"-");
				sb.append("$");
				sb.append("设备名称_");
				sb.append(itAssetVo.getAssetName()!=null?itAssetVo.getAssetName():"-");
				sb.append("$");
				sb.append("品牌_");
				sb.append(itAssetVo.getBrandName()!=null?itAssetVo.getBrandName():"-");
				sb.append("$");
				sb.append("设备型号_");
				sb.append(itAssetVo.getModelName()!=null?itAssetVo.getModelName():"-");
				sb.append("$");
				sb.append("RFID号_");
				sb.append(itAssetVo.getRfidNo()!=null?itAssetVo.getRfidNo():"-");
				sb.append("$");
				sb.append("2.4G标签号");
				sb.append(itAssetVo.getRfidNo1()!=null?itAssetVo.getRfidNo1():"-");
				sb.append("$");
				sb.append("所在机房_");
				sb.append(itAssetVo.getRoomName()!=null?itAssetVo.getRoomName():"-");
				sb.append("$");
				sb.append("所在位置_");
				sb.append(itAssetVo.getRackName()!=null?itAssetVo.getRackName():"-");
				sb.append("$");
				sb.append("U位_");
				sb.append(itAssetVo.getStartU()!=null?itAssetVo.getStartU():"-");
				sb.append("-");
				sb.append(itAssetVo.getEndU()!=null?itAssetVo.getEndU():"-");
				sb.append("$");
				sb.append("状态_");
				sb.append(itAssetVo.getStatus()!=null?itAssetVo.getStatus()==1?"在库":itAssetVo.getStatus()==2?"已上架":itAssetVo.getStatus()==3?"已报废":"-":"-");
				sb.append("$");
				sb.append("是否借用_");
				sb.append(itAssetVo.getIsBorrow()!=null?itAssetVo.getIsBorrow()?"借用":"未借用":"-");
				sb.append("$");
				sb.append("是否分配_");
				sb.append(itAssetVo.getIsDistribute()!=null?itAssetVo.getIsDistribute()?"已分配":"未分配":"-");
				sb.append("$");
				sb.append("管理部门_");
				sb.append(itAssetVo.getManageBranchName()!=null?itAssetVo.getManageBranchName():"-");
				sb.append("$");
				sb.append("领用部门_");
				sb.append(itAssetVo.getBranchName()!=null?itAssetVo.getBranchName():"-");
				sb.append("$");
				sb.append("领用人_");
				sb.append(itAssetVo.getUserName()!=null?itAssetVo.getUserName():"-");
				sb.append("$");
				sb.append("供货方_");
				sb.append(itAssetVo.getSupplier()!=null?itAssetVo.getSupplier():"-");
				sb.append("$");
				sb.append("联系电话_");
				sb.append(itAssetVo.getContactsPhone()!=null?itAssetVo.getContactsPhone():"-");
				sb.append("$");
				sb.append("联系人_");
				sb.append(itAssetVo.getContacts()!=null?itAssetVo.getContacts():"-");
				sb.append("$");
				sb.append("售后电话_");
				sb.append(itAssetVo.getHotline()!=null?itAssetVo.getHotline():"-");
				sb.append("$");
				sb.append("合同号_");
				sb.append(itAssetVo.getContractNo()!=null?itAssetVo.getContractNo():"-");
				sb.append("$");
				sb.append("发票号_");
				sb.append(itAssetVo.getInvoiceNo()!=null?itAssetVo.getInvoiceNo():"-");
				sb.append("$");
				sb.append("价钱_");
				sb.append(itAssetVo.getPrice()!=null?itAssetVo.getPrice():"-");
				sb.append("$");
				sb.append("购置时间_");
				sb.append(itAssetVo.getBuyTime()!=null?dateFormat.format(itAssetVo.getBuyTime()):"-");
				sb.append("$");
				sb.append("报修到期时间_");
				sb.append(itAssetVo.getExpirationTime()!=null?dateFormat.format(itAssetVo.getExpirationTime()):"-");
				String tvsb = sb.toString();
				String[] tvsbArray = tvsb.split("\\$");
				List<AssetExlVo> entityList = new ArrayList<AssetExlVo>();
				if (tvsbArray.length>0) {
					for (String tv : tvsbArray) {
						AssetExlVo entity = new AssetExlVo();
						String[] tvArray = tv.split("_");
						entity.setTitle(tvArray[0]);
						entity.setValue(tvArray.length==1?"-":tvArray[1]);
						entityList.add(entity);
					}
				}
				if (dataNum == 1) {//若为第一次循环
					//把list<entity>中的title按list的顺序放入String【】，调用：setExcelListTitle
					String[] titleArray = new String[entityList.size()];
					for (int i = 0; i < entityList.size(); i++) {
						titleArray[i] = entityList.get(i).getTitle();
					}
					dExlUtile.addRow(titleArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
					//dExlUtile.setExcelListTitle(titleArray);
					//把list<entity>中的value按list的顺序放入String【】，调用：addRow
					String[] valueArray = new String[entityList.size()];
					for (int i = 0; i < entityList.size(); i++) {
						valueArray[i] = entityList.get(i).getValue();
					}
					dExlUtile.addRow(valueArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
					dataNum++;
				}else {
					//把list<entity>中的value按list的顺序放入String【】，调用：addRow
					String[] valueArray = new String[entityList.size()];
					for (int i = 0; i < entityList.size(); i++) {
						valueArray[i] = entityList.get(i).getValue();
					}
					dExlUtile.addRow(valueArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "");
				}
			}
			if (vo.getStatisticsCondition()!=null&&!"".equals(vo.getStatisticsCondition())) {
				String[] str = vo.getStatisticsCondition().split(",");
				for (String s : str) {
					String sheetName  ="";
					String xName = "";
					DefaultCategoryDataset mDataset = null;
					if (s.equals("资产分类")) {//资产分类
						sheetName = "资产分类图";
						xName = "分类";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetTypeId(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无分类");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("资产品牌")) {//资产品牌
						sheetName = "资产品牌图";
						xName = "品牌";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetBrandId(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无品牌");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("资产型号")) {//资产型号
						sheetName = "资产型号图";
						xName = "型号";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetModelId(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无型号");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("机房")) {//机房
						sheetName = "机房图";
						xName = "机房";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetRoomId(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无机房");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("购置时间")) {//购置时间
						sheetName = "购置时间图";
						xName = "购置时间";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetBuyTime(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无购置时间");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("维保到期时间")) {//维保到期时间
						sheetName = "维保到期时间图";
						xName = "维保到期时间";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetExpirationTime(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无维保到期时间");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("管理部门")) {//管理部门
						sheetName = "管理部门图";
						xName = "管理部门";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetManageBranch(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无管理部门");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					if (s.equals("使用部门")) {//使用部门
						sheetName = "使用部门图";
						xName = "使用部门";
						List<ExlChartVo> dataList = itassetMapper.qryChartDataForAssetUserBranch(params);
						if (dataList != null && dataList.size()>0) {
							mDataset = new DefaultCategoryDataset();
							for (ExlChartVo exlChartVo : dataList) {
								if (exlChartVo.getColumnKey()==null || "".equals(exlChartVo.getColumnKey())) {
									exlChartVo.setColumnKey("无使用部门");
								}
								mDataset.addValue(exlChartVo.getValue(), exlChartVo.getRowKey(), exlChartVo.getColumnKey());
							}
						}
					}
					
					if (mDataset!=null) {
						dExlUtile.addSheet(sheetName);
						String imgPath = dExlUtile.drawBarOnSheet(sheetName, xName, "数量", mDataset);
						imgPathList.add(imgPath);
					}
				}
			}
		}else {
			String[] errArray = new String[1];
			errArray[0] = "查询结果为空";
			dExlUtile.addRow(errArray, BorderLineStyle.THIN,jxl.format.Alignment.CENTRE, "bold");
		}
		dExlUtile.reportExcel();
		if (imgPathList!=null && imgPathList.size()>0) {
			for (String s : imgPathList) {
				File imgFile = new File(s);
				if(imgFile.exists() && imgFile.isFile()){  
					imgFile.delete();  
			    }
			}
		}
		ReportFile rf = new ReportFile();
		rf.setFilePath(dbFilePath);
		rf.setTempletId(id);
		rf.setId(IdWorker.getId());
		rf.setCreateTime(new Date());
		reportMapper.insertReportFile(rf);
	}

}
